import 'package:app/components/index.dart';
import 'package:app/util/StringUtils.dart';
import 'package:flutter/material.dart';

import '../../api/pick/index.dart';

class PickDetailSerach extends StatefulWidget {
  const PickDetailSerach({
    super.key,
  });

  @override
  State<PickDetailSerach> createState() => _PickDetailSerachState();
}

class _PickDetailSerachState extends State<PickDetailSerach>
    with SingleTickerProviderStateMixin {
  //拣配单号
  String _pickCode = '';

  //状态
  String _status = '';

  //状态描述
  String _statusName = '';

  //拣配人
  String _pickUser = '';

  //隐藏标识
  bool isHide = true;

  List<String> undoneStatus = ['0', '10', '20'];

  List<dynamic> _list = [];

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      init();
      setParams(ModalRoute.of(context)!.settings.arguments);
    });
  }

  void init() {
    Future.delayed(Duration.zero).then((value) async {
      findPickDetail(ModalRoute.of(context)!.settings.arguments);
    });
  }

  /// 设置参数
  void setParams(params) {
    if (params != null) {
      setState(() {
        params['pickCode'] != null ? _pickCode = params['pickCode'] : "";
        params['status'] != null ? _status = params['status'] : "";
        params['statusName'] != null ? _statusName = params['statusName'] : "";
        params['pickUser'] != null ? _pickUser = params['pickUser'] : "";
      });
    }
  }

  void findPickDetail(params) {
    if (params != null) {
      PickApi.of(context)
          .pickDetailByPickId(params['pickId'].toString())
          .then((res) {
        if (StringUtils.isNotNull(res.data)) {
          setState(() {
            _list = res.data['data']['rows'];
            for (var item in _list) {
              item['isHide'] = true;
            }
          });
          hideKeyBorder();
        }
      });
    }
  }

  void hideKeyBorder() {
    FocusScopeNode currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
      FocusManager.instance.primaryFocus?.unfocus();
    }
  }

  void go(String path) {
    Navigator.pushNamed(context, path);
  }

  Color getStatusBgColor(String code) {
    switch (code) {
      case '0':
        return Colors.amber;
      case '10':
        return Colors.amber;
      case '20':
        return Colors.amber;
      case '30':
        return Colors.greenAccent;
      case '99':
        return Colors.grey;
      default:
        return Colors.white;
    }
  }

  Color getLackMaterialStatusBgColor(code) {
    switch (code) {
      case '0':
        return Colors.red;
      case '1':
        return const Color.fromARGB(255, 69, 228, 194);
      default:
        return Colors.white;
    }
  }

  geterpInvbillTypeBgColor(code) {
    switch (code) {
      case '1':
        return const Color.fromARGB(255, 69, 228, 194);
      case '2':
        return Colors.red;
      case '3':
        return const Color.fromARGB(255, 69, 228, 194);
      default:
        return Colors.white;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBars(context: context, title: '拣配详细'),
      body: GestureDetector(
          onTap: hideKeyBorder,
          child: Column(
            children: [
              Horizontal(items: [
                HorizontalItem(
                  label: '拣配单号：',
                  value: _pickCode,
                  fontWeight: FontWeight.w600,
                )
              ]),
              Horizontal(items: [
                HorizontalItem(
                    label: '状态：',
                    value: _statusName,
                    fontWeight: FontWeight.w600,
                    fontColor: Colors.white,
                    valueBackground: getStatusBgColor(_status)),
                HorizontalItem(
                  label: '拣配人：',
                  value: _pickUser,
                )
              ]),
              Expanded(
                child: ListContainer(
                  length: _list.length,
                  children: (index) {
                    return [
                      ListTitle(
                        context: context,
                        label: _list[index]['itemName'],
                      ),
                      Horizontal(items: [
                        HorizontalItem(
                          label: '单据号：',
                          value: _list[index]['erpInvbillCode'],
                        ),
                        HorizontalItem(
                          label: '托盘：',
                          value: _list[index]['container'],
                        ),
                      ]),
                      Horizontal(items: [
                        HorizontalItem(
                          label: '班组编号：',
                          value: _list[index]['wcCode'],
                        ),
                        HorizontalItem(
                          label: '班组名称：',
                          value: _list[index]['wcName'],
                        )
                      ]),
                      Horizontal(items: [
                        HorizontalItem(
                          label: '物料编码：',
                          value: _list[index]['itemCode'],
                        ),
                        HorizontalItem(
                          label: '物料工序号：',
                          value: _list[index]['itemOpNo'],
                        )
                      ]),
                      Horizontal(items: [
                        HorizontalItem(
                          label: '物料任务号：',
                          value: _list[index]['itemWorkcode'],
                        ),
                      ]),
                      Offstage(
                          offstage: _list[index]['isHide'],
                          child: Column(
                            children: [
                              Horizontal(items: [
                                HorizontalItem(
                                  label: '配送终点：',
                                  value: _list[index]['endLocationName'],
                                ),
                                HorizontalItem(
                                    label: 'Erp单据类型：',
                                    value: _list[index]['erpInvbillTypeName'],
                                    fontColor: Colors.white,
                                    valueBackground: geterpInvbillTypeBgColor(
                                        _list[index]['erpInvbillType']
                                            .toString())),
                              ]),
                              Horizontal(items: [
                                HorizontalItem(
                                  label: '优先级：',
                                  value: _list[index]['priority'] != null
                                      ? _list[index]['priority'].toString()
                                      : '',
                                ),
                                HorizontalItem(
                                    label: '缺料状态：',
                                    value: _list[index]
                                        ['lackMaterialStatusName'],
                                    fontColor: Colors.white,
                                    valueBackground:
                                        getLackMaterialStatusBgColor(
                                            _list[index]['lackMaterialStatus']
                                                .toString()))
                              ]),
                              Horizontal(items: [
                                HorizontalItem(
                                  label: '数量：',
                                  value: _list[index]['itemQty'].toString(),
                                ),
                                HorizontalItem(
                                  label: '单位：',
                                  value: _list[index]['itemUnit'],
                                  fontColor: Colors.white,
                                  valueBackground: _list[index]['itemUnit'] !=
                                          null
                                      ? const Color.fromARGB(255, 69, 228, 194)
                                      : Colors.white,
                                ),
                              ])
                            ],
                          )),
                      GestureDetector(
                        onTap: () {
                          setState(() {
                            _list[index]['isHide'] = !_list[index]['isHide'];
                          });
                        },
                        child: Container(
                            width: MediaQuery.of(context).size.width,
                            padding: const EdgeInsets.fromLTRB(0, 5, 0, 5),
                            child: Row(
                              children: [
                                const Expanded(flex: 5, child: Text("")),
                                Expanded(
                                    flex: 1,
                                    child: Text(
                                      _list[index]['isHide'] ? "展开" : "收起",
                                      style: const TextStyle(
                                          fontWeight: FontWeight.w600,
                                          color: Color.fromARGB(
                                              255, 69, 228, 194)),
                                    )),
                                Expanded(
                                    flex: 1,
                                    child: _list[index]['isHide']
                                        ? const Icon(Icons.keyboard_arrow_down,
                                            color: Color.fromARGB(
                                                255, 69, 228, 194))
                                        : const Icon(Icons.keyboard_arrow_up,
                                            color: Color.fromARGB(
                                                255, 69, 228, 194))),
                                const Expanded(flex: 5, child: Text("")),
                              ],
                            )),
                      )
                    ];
                  },
                ),
              ),
            ],
          )),
    );
  }
}
